var lastsel=0;
jQuery("#tableTours").jqGrid({
   	url:'/admin/show',
        editurl:'/admin/edittour',
	datatype: "json",
        mtype: 'POST',
   	colNames:['ID','№','Название тура', 'Дней', 'Ночей', 'Проживание', 'Экскурсия', 'Экскурсия', 'Экскурсия', 'Экскурсия', 'Экскурсия', 'Экскурсия', 'Подарок', 'Фото','>'],
   	colModel:[
   		{name:'id',index:'id', width:55, align:"center", hidden:true},
   		{name:'number',index:'number', width:55, align:"center",editable:true,editrules:{required:true,integer:true}},
   		{name:'tourTitle',index:'tourTitle', width:250,editable:true,editrules:{required:true},searchoptions: { sopt: ['eq','ne','bw','ew'] }},
   		{name:'days',index:'days', width:55, align:"center",editable:true,editrules:{required:true,integer:true},searchoptions: { sopt: ['eq','ne','lt','gt'] }},
   		{name:'nights',index:'nights', width:55, align:"center",editable:true,editrules:{required:true,integer:true},searchoptions: { sopt: ['eq','ne','lt','gt'] }},
		{name:'home',index:'home', width:80, align:"center",editable:true,editrules:{required:false},searchoptions: { sopt: ['eq','ne','lt','gt'] }},
		{name:'ekskurs1',index:'ekskurs1', width:80, align:"center",editable:true,edittype:"select",editoptions:{value:eksk},editrules:{required:false},searchoptions: { sopt: ['eq','ne','lt','gt'] }},
		{name:'ekskurs2',index:'ekskurs2', width:80, align:"center",editable:true,edittype:"select",editoptions:{value:eksk},editrules:{required:false},searchoptions: { sopt: ['eq','ne','lt','gt'] }},
		{name:'ekskurs3',index:'ekskurs3', width:80, align:"center",editable:true,edittype:"select",editoptions:{value:eksk},editrules:{required:false},searchoptions: { sopt: ['eq','ne','lt','gt'] }},
		{name:'ekskurs4',index:'ekskurs4', width:80, align:"center",editable:true,edittype:"select",editoptions:{value:eksk},editrules:{required:false},searchoptions: { sopt: ['eq','ne','lt','gt'] }},
		{name:'ekskurs5',index:'ekskurs5', width:80, align:"center",editable:true,edittype:"select",editoptions:{value:eksk},editrules:{required:false},searchoptions: { sopt: ['eq','ne','lt','gt'] }},
		{name:'ekskurs6',index:'ekskurs6', width:80, align:"center",editable:true,edittype:"select",editoptions:{value:eksk},editrules:{required:false},searchoptions: { sopt: ['eq','ne','lt','gt'] }},
		{name:'podarok',index:'podarok', width:80, align:"center",editable:true,editrules:{required:false},searchoptions: { sopt: ['eq','ne','lt','gt'] }},
		{name:'img',index:'img', width:60, align:"center",editable:false,editrules:{required:false},searchoptions: { sopt: ['eq','ne','lt','gt'] }},

		{name:'go',index:'go', width:20, align:"center",editable:false}

   	],
   	rowNum:10,
   	rowList:[10,20,30],
   	pager: '#pagerTours',
   	sortname: 'id',
    viewrecords: true,
    sortorder: "asc",
    caption:"Туры",
    subGrid: true,
    onSelectRow: function (id) {
                if (id && id !== lastsel) {

                   	jQuery('#tableTours').jqGrid('restoreRow', lastsel);
                	jQuery('#tableTours').jqGrid('editRow', id, true);
                    lastsel = id;
                }
            },
    width:1300,
    height:300,
    subGridRowExpanded: function(subgrid_id, row_id) 
    	{
			var subgrid_table_id, pager_id;
			subgrid_table_id = subgrid_id+"_t";
			pager_id = "p_"+subgrid_table_id;
			$("#"+subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table><div id='"+pager_id+"' class='scroll'></div>");
			jQuery("#"+subgrid_table_id).jqGrid(
			{
				url:"/admin/showekskurssubgrid/"+row_id,
				datatype: "json",
				colNames: ['Название','Описание'],
				colModel: [
					{name:"name",index:"name",width:80,key:true,editable:true,edittype:"select",editoptions:{value:eksk},editrules:{required:false}},
					{name:"desc",index:"desc",width:130}
					
				],
			   	rowNum:20,
			   	pager: pager_id,
			   	sortname: 'Ekskurs',
			    sortorder: "asc",
			    caption:"Экскурсии",
			    height: '100%',
			    width: '600'
			});
			jQuery("#"+subgrid_table_id).jqGrid('navGrid',"#"+pager_id,{edit:true,add:true,del:true});
    	}
});
jQuery("#tableTours").jqGrid('navGrid','#pagerTours',{edit:true,add:true,del:true});
/**
 *
 * @access public
 * @return void
 **/




function show(id){
jQuery("#mm"+id).hide();
jQuery("#ma"+id).show();
}
function hide(id){
jQuery("#mm"+id).show();
jQuery("#ma"+id).hide();
}
/**
 *
 * @access public
 * @return void
 **/
function load_photo(id){
    jQuery("#tourfoto").text(id+"");
	jQuery("#load").show();
}
/**
 *
 * @access public
 * @return void
 **/
function del_photo(id){
if(confirm("Вы действительно хотите удалить фотографию?")==true) {
	var url="/admin/delfoto/"+id;
  	$.ajax({
	    url: url,             // указываем URL и
	    dataType : "json",                     // тип загружаемых данных
	    success: function (data, textStatus) { // вешаем свой обработчик на функцию success
	 		 		jQuery('#tableTours').trigger("reloadGrid");
	    			}
	});
	}
}
  var f=0;
 function ajaxFileUpload(id)
    {
        $("#loading")
        .ajaxStart(function(){
            $(this).show();
            f=0;
        })
        .ajaxComplete(function(){
            $(this).hide();
            if(f==0)
            {
           	 jQuery("#tableTours").trigger("reloadGrid");
            	jQuery("#load").hide();
            	f=1;
            }
        });

        $.ajaxFileUpload
        (
            {
                url:'/admin/tourfoto/'+id,
                secureuri:false,
                fileElementId:'foto',
                dataType: 'xml',
                success: function (data, status)
                {
                    if(typeof(data.error) != 'undefined')
                    {
                        if(data.error != '')
                        {
                            alert(data.error);
                        }else
                        {
                            alert(data.msg);
                        }
                    }
                },
                error: function (data, status, e)
                {
                    alert(e);
                }
            }
        )

        return false;

    }





